﻿using MangoMes.Commont.Domain.SellStocks;
using MangoMes.Commont.DTOs;
using MangoMes.Write.API.Applications.ProductionWrite;
using MangoMes.Write.API.Applications.PurchaseStorags;
using MangoMes.Write.API.Applications.QRUPCWrite;
using MangoMes.Write.API.Applications.SellStockSearchWrite;
using MangoMes.Write.API.Applications.SupplierSearchWrite;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using SixLabors.Fonts;
using SkiaSharp.QrCode.Image;
using SkiaSharp;
using System.Globalization;
using QRCoder;

namespace MangoMes.Write.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserLiController : ControllerBase
    {
        private readonly IMediator _mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public UserLiController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 供应商新增
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> CreateSupplier(SupplierCreateCommand info)
        {
            try
            {
                var cou = await _mediator.Send(info);
                return cou;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 供应商逻辑删除
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> DeleteSupplier(SellStockDeleteCommand cmd)
        {
            try
            {
                var cou = await _mediator.Send(cmd);
                return cou;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 销售出库单逻辑删除
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> DeleteSellStock(SellStockDeleteCommand cmd)
        {
            try
            {
                var cou = await _mediator.Send(cmd);
                return cou;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 销售出库单新增
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> CreateSellStock(SellStockCreateCommand cmd)
        {
            try
            {
                var cou = await _mediator.Send(cmd);
                return cou;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 销售出库单修改
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> UpdateSellStock(SellStockUpdateCommand cmd)
        {
            try
            {
                var cou = await _mediator.Send(cmd);
                return cou;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 产品销售新增
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> CreateProductsSale(ProductsSaleCreateCommand cmd)
        {
            try
            {
                var cou = await _mediator.Send(cmd);
                return cou;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 执行出库
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> CreateStockRemoval(StockRemovalCreateCommand cmd)
        {
            try
            {
                var cou = await _mediator.Send(cmd);
                return cou;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 生产报工新增
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> CreateProduction(ProductionCreateCommand cmd)
        {
            try
            {
                var cou = await _mediator.Send(cmd);
                return cou;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 生产报工逻辑删除
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> DeleteProduction(ProductionDeleteCommand cmd)
        {
            try
            {
                var cou = await _mediator.Send(cmd);
                return cou;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 生产报工修改
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> UpdateProduction(ProductionUpdateCommand cmd)
        {
            try
            {
                var cou = await _mediator.Send(cmd);
                return cou;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 审批修改
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> UpdProduction(ProductionUpdCommand cmd)
        {
            try
            {
                var cou = await _mediator.Send(cmd);
                return cou;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 生产报工导入
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> ImportPurchaseStorage(IFormFile file)
        {
            ResultsDTO res = new ResultsDTO();
            var exName = Path.GetExtension(file.FileName);
            if (exName == ".xls" || exName == ".xlsx")
            {

            }
            else
            {
                res.Message = "传输的文件格式有误";
                return res;
            }
            using (var ms = new MemoryStream())
            {

                file.CopyTo(ms);
                ms.Position = 0;
                IWorkbook wb = null;
                if (exName == ".xls")//创建87-2003
                {
                    wb = new HSSFWorkbook(ms);
                }
                else
                {
                    wb = new XSSFWorkbook(ms);
                }
                ISheet sheet = wb.GetSheetAt(0);
                int rowNum = sheet.LastRowNum;
                int cellNum = sheet.GetRow(0).LastCellNum;
                ProductionCreateCommand order = new ProductionCreateCommand();
                order.ProductionWorkId = 0;
                for (int i = 1; i <= rowNum; i++)
                {

                    order.ProductionWorkNo = sheet.GetRow(i).GetCell(0).ToString();
                    order.ProductionTypeId = Convert.ToInt32(sheet.GetRow(i).GetCell(1).ToString());
                    order.ProductionWorkOrdersID = Convert.ToInt32(sheet.GetRow(i).GetCell(2).ToString());
                    order.PickingNode = sheet.GetRow(i).GetCell(3).ToString();
                    order.ProductionWorkNum = Convert.ToInt32(sheet.GetRow(i).GetCell(4).ToString());
                    order.departmentId = Convert.ToInt32(sheet.GetRow(i).GetCell(5).ToString());
                    var a = sheet.GetRow(i).GetCell(6).ToString();
                    string[] formats = { "dd-M月-yyyy", "yyyy-MM-dd" };
                    DateTime thisDate;
                    foreach (var format in formats)
                    {
                        if (DateTime.TryParseExact(a, new[] { format }, CultureInfo.InvariantCulture, DateTimeStyles.None, out thisDate))
                        {
                            order.ProductionWorkTime = Convert.ToDateTime(thisDate.ToString("yyyy-MM-dd"));
                            break;
                        }
                    }
                    order.ProductionStart = Convert.ToInt32(sheet.GetRow(i).GetCell(7).ToString());
                    order.ProductionWorkRemark = sheet.GetRow(i).GetCell(8).ToString();
                    order.ProductionWorkState = 0;
                    res = await CreateProduction(order);
                }
                return res;
            }
        }
        /// <summary>
        /// 赋码管理新增
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> Createqucode(QUCodeCreateCommand cmd)
        {
            try
            {
                var cou = await _mediator.Send(cmd);
                return cou;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 修改赋码管理
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> Updatequcode(QUCodeUpdateCommand cmd)
        {
            try
            {
                var cou = await _mediator.Send(cmd);
                return cou;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
